#pragma once

class ThreadSyncer : public LObj
{	// manager daq buffers;
	typedef struct 
	{
		bool		islock;
		int			waittime;
	} userinf_t;
	typedef struct 
	{
		int			getnum;
		bool		islock;
		int			waittime;

		userinf_t *	infs;
		int			ifnum;
	} syuser_t;
	typedef struct
	{
		syuser_t *	user;
		int			num;
	}user_t;
	typedef struct
	{
		HANDLE *	mutex;
		int			num;
	}mutex_t;
public:
	ThreadSyncer(void);
	~ThreadSyncer(void);

	int get( int id );		// 140503;
	void rls( int id, bool next );	//140503;

	//int		Init( int bufnum, int usernum );
	int		Get( int id, int *pnum );
	int		getInc( int id, int *pnum);
	int		Release( int id, bool next );
	void	Reset( int id );
	
	int		Get( int id, int num );
	int		Release( int id, int num, bool next ); 

	void	setWaitTime(int user, int tm);
	void	SetWaittime(int user, int tm, int num ); // not used yet,for individal set which buf;
	void	setBufnum( int num) { mbufnum = num; }
	void	setUsernum( int num ) { musernum = num; }

private:
	// lobj;
	int init();
	void exit();

	void	createMutex(mutex_t * mutex, int num);
	void	deleteMutex(mutex_t * mutex);
	void	createUser(user_t* user, int num, int bufnum);
	void	deleteUser(user_t * user);

	
	mutex_t mmutex;
	user_t  muser;
	int		mbufnum;
	int		musernum;

	void incGetnum( syuser_t * user ){
		if ( ++user->getnum >= mmutex.num )
		{
			user->getnum = 0;
		}
	}
	/* // 2013-07-09;
	HANDLE *	mmutexs;
	int			mmutexnum;
	syuser_t *	musers;
	int			musernum;
	*/
};
